/*
*** (C) 2008-2009 UCMai.com ***

Subj: 帮助内容管理
Auth: zhouyt
Date: 2008-07-31
*/

c.pgsubject = '帮助内容';

var SysHelpDataCore = function(){
	//修改项字段
	this.formReader = [
		{name:'CatID'},
		{name:'CatName'},
		{name:'Subject'},
		{name:'Keyword'},
		{name:'Content'},
		{name:'ViewCount'},
		{name:'ListIndex'},
		{name:'Revision'},
		{name:'OptionSet'},
		{name:'Remark'},
		{name:'Status'},
		{name:'Content'}
	];

	var CatStore = new Ext.data.Store({
		autoLoad:true,
		proxy:new Ext.data.HttpProxy({url:_S_HelpCat_getList}),
		reader:new Ext.data.JsonReader({
			fields:[
				{name:'CatID',type:'int'},
				{name:'CatName'}
			],
			root:'records'
		})
	});

	//表单项
	this.formItems = [
		{xtype:'hidden',name:'a',id:'se_o_cmdid',value:'0'},
		{xtype:'hidden',name:'li',id:'se_o_li',value:'0'},
		{xtype:'hidden',name:'ln',id:'se_o_ln',value:''},
		{xtype:'hidden',name:'ArticleID',id:'ArticleID',value:'0'},
		{
			xtype:'combo',
			store:CatStore,
			displayField:'CatName',
			valueField:'CatID',
			fieldLabel:'类别名称',
			name:'CatID',
			hiddenName:'CatID',
			id:'se_o_gameid',
			mode:'local',
			triggerAction: 'all',
			blankText:'请选择所属类别',
			emptyText:'选择所属类别',
			allowBlank:false,
			anchor:'50%',
			editable: false
		},
		{
			fieldLabel: '标题',
			name: 'Subject',
			id: 'se_o_subject',
			anchor: '85%',
			maxLength: '120'
		},
		{
			fieldLabel: '标签',
			name: 'Keyword',
			id: 'se_o_keyword',
			anchor: '85%',
			maxLength: '32',
			allowBlank: false
		},
		{
			xtype:'htmleditor',
			name: 'Content',
			id:'se_o_content',
			fieldLabel:'正文',
			width: 530,
			height: 300,
			frame: true,
			layout: 'fit'
		},
		{
			fieldLabel: '备注',
			name: 'Remark',
			id: 'se_o_remark',
			anchor: '85%',
			maxLength: '32',
			allowBlank: true
		}
	];

	//查看项字段
	this.viewReader = [
		{name:'CatID'},
		{name:'Subject'},
		{name:'Keyword'},
		{name:'Content'},
		{name:'ViewCount'},
		{name:'ListIndex'},
		{name:'Revision'},
		{name:'OptionSet'},
		{name:'Remark'},
		{name:'Status'}
	];

	//查看项
	this.viewItems = [
		{xtype:'hidden',name:'a',id:'se_o_cmdid',value:'0'},
		{xtype:'hidden',name:'li',id:'se_o_li',value:'0'},
		{xtype:'hidden',name:'ln',id:'se_o_ln',value:''},
		{xtype:'hidden',name:'ArticleID',id:'ArticleID',value:'0'},
		{
			xtype:'htmleditor',
			name: 'Content',
			id:'se_o_content',
			fieldLabel:'正文',
			width: 600,
			height: 350,
			frame: true,
			layout: 'fit'
		}
	];

	//Grid列头
	this.sm = new Ext.grid.CheckboxSelectionModel();
	this.gridHeader = [
		new Ext.grid.RowNumberer(),
		this.sm,
		{header:'类别名称',dataIndex:'CatName'},
		{header:'标题',dataIndex:'Subject',width: 300},
		{header:'标签',dataIndex:'Keyword'},
		{header:'浏览数',dataIndex:'ViewCount'},
		{header:'版本',dataIndex:'Revision'},
		{header:'备注',dataIndex:'Remark'},
		{header:'状态',dataIndex:'Status',renderer:function(value){

			return '<b><font color="'+  c.aLock[value][1] +'">'+  c.aLock[value][0] +'</font></b>'
		}}
	];

	//Grid绑定的字段
	this.gridStore = [
		{name:'ArticleID'},
		{name:'CatID'},
		{name:'CatName'},
		{name:'Subject'},
		{name:'Keyword'},
		{name:'Content'},
		{name:'ViewCount'},
		{name:'ListIndex'},
		{name:'Revision'},
		{name:'OptionSet'},
		{name:'Remark'},
		{name:'UpdateOn'},
		{name:'CreateOn'},
		{name:'Status'}
	];

	//查看
	this.showView = function(){
		//openBrWindow('海外客服FAQ','s/SysHelpCat_faq_getlist.asp','ServerEditDemand','',700,350,200,50);
		zyt.Permit();
		var selectedId = this.getSelectedIds();
		this.form = new Ext.FormPanel({
				defaults: {width:630},
				labelAlign:'left',
				bodyStyle:'padding:5px',
				frame:true,
				labelWidth:60,
				reader : new Ext.data.JsonReader({
					root : 'record',
					success: '@success'
				},this.viewReader),
				items:[
					{
						layout:'column',
						border:false,
						labelSeparator:'：',
						items:this.viewItems
				}]
		});
		this.window = new Ext.Window({
			xtype:'window',
			title: c.vd(),
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			layout:'fit',
			width:650,
			height:450,
			closeAction:'hide',
			plain: true,
			items:[this.form],
			buttons: [{
				text: c.close,
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
			}]
		});
		this.window.show();

		/* local mode */
		this.form.getForm().setValues(this.getSelectionModel().getSelected().data);
		
	};

	//编辑
	this.showWindow = function(str){
		zyt.Permit();
		this.form = new Ext.FormPanel({
				labelAlign:'left',
				bodyStyle:'padding:5px',
				frame:true,
				labelWidth:60,
				reader : new Ext.data.JsonReader({
					root : 'record',
					success: '@success'
				},this.formReader),
				items:[
					{
						layout:'form',
						defaultType:'textfield',
						border:false,
						labelSeparator:'：',
						items:this.formItems
				}]
		});
		this.form.getForm().reset();
		//if (!this.window){
		this.window = new Ext.Window({
			xtype:'window',
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			layout:'fit',
			modal: 'true',
			width:650,
			height:550,
			closeAction:'hide',
			plain: true,
			items:[this.form],
			keys: {
				key: Ext.EventObject.ENTER,
				fn: save.createDelegate(this,[str])
			},
			buttons: [{
				id: 'btnSave',
				text: c.bsave,
				handler: save.createDelegate(this,[str]),
				disabled: false
			},{
				text: c.bcancel,
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
			}]
		});
		this.window.on('hide',function(){
			this.form.destroy();
		}.createDelegate(this));
		//}

		//this.window.show(this.getEl());
		this.window.show();
		this.form.findById('se_o_subject').focus(false,true);
		this.window.setTitle((str)?c.ad():c.md());
		if(!str){
			/* local mode */
			this.form.getForm().setValues(this.getSelectionModel().getSelected().data);
		}
	};

	//保存
	var save = function(str){
		if (!this.form.getForm().isValid()){
			return;
		}

		var selectedId = this.getSelectedIds();
		if(str){selectedId = 0;}

		if(selectedId>0){
			this.form.findById('se_o_cmdid').setValue(6);
			this.form.findById('ArticleID').setValue(selectedId);
		}else{
			this.form.findById('se_o_cmdid').setValue(5);
		}

		showLoading(c.g(c.e.save),c.loading);
		zyt.ajax(_C_SysHelpData_S,this.form.getForm().getValues(), function(){
			hideLoading();
			this.window.hide();
			var act = selectedId>0?c.modify:c.add;
			Ext.HandleTip.msg(c.opres,c.item + act + c.ok);
			this.reload();
		}.createDelegate(this));
	};

	//删除
	var remove = function(){
		zyt.Permit();
		zyt.confirmEx(c.del, "记录", function(){
			var delids = this.getSelectedIds();
			zyt.ajax(_C_SysHelpData_X_Del, {a:4,aid:delids}, function(){
				Ext.HandleTip.msg(c.opres,c.selitem + c.del + c.ok);
				this.reload();
			}.createDelegate(this));
		}.createDelegate(this));
	};

	//锁定
	var lock = function(str){
		zyt.Permit();
		var act = str?c.lock:c.unlock;
		Ext.MessageBox.confirm(c.info,'您确定要将所选' + c.pgsubject + act,function(btn){
			if(btn=='yes'){
				var delids = this.getSelectedIds();
				zyt.ajax(_C_SysHelpData_X_Lock, {a:2,aid:delids}, function(){
					Ext.HandleTip.msg(c.opres,c.selitem + act + c.ok);
					this.reload();
				}.createDelegate(this));
			}
		}.createDelegate(this));
	};


	SysHelpDataCore.superclass.constructor.call(this, {
		region: 'center',
		keyField: 'ArticleID',
		url:_C_SysHelpData_X,
		viewConfig: {forceFit:true},
		tbar:[{
				id:'btnView',
				text:c.view,
				iconCls:'view',
				disabled:true,
				handler: this.showView.createDelegate(this, [true])
			}, '-',
			{
				id:'btnAdd',
				text:c.add,
				iconCls:'add',
				handler: this.showWindow.createDelegate(this, [true])
			}, '-',
			{
				id:'btnModify',
				text:c.modify,
				iconCls:'modify',
				disabled:true,
				handler:this.showWindow.createDelegate(this, [false])
			},'-',{
				id:'btnDel',
				text:c.del,
				iconCls:'remove',
				disabled:true,
				handler:remove.createDelegate(this)
			},'-',{
				id:'btnLock',
				text:c.lock,
				iconCls:'lock',
				disabled:true,
				handler:lock.createDelegate(this)
			}],
		page: true,
		ready: function(){
					//alert(this.store.jsonData);
			//var acl = this.gridReader.jsonData.Acl;
			this.store.on('load',function(){
				if(typeof acl!="undefined"){
					var gridTb = this.getTopToolbar();
					if(acl[0].aNew===0){
						gridTb.items.get(0).hide();
						gridTb.items.get(1).hide();
						messageContextMenu.items.get(1).hide();
					}
					if(acl[0].aEdit===0){
						gridTb.items.get(2).hide();
						gridTb.items.get(3).hide();
						messageContextMenu.items.get(2).hide();
					}
					if(acl[0].aDel===0){
						gridTb.items.get(4).hide();
						gridTb.items.get(5).hide();
						messageContextMenu.items.get(3).hide();
					}
				}
			}.createDelegate(this));
		}
	});

	this.getSelectionModel().on('selectionchange', function(){
		var haveselected = this.getSelectedIds();
		if(haveselected!='0'){
			if(typeof haveselected==='number'){
				Ext.getCmp('btnModify').setDisabled(false);
				Ext.getCmp('btnView').setDisabled(false);
			}else{
				Ext.getCmp('btnModify').setDisabled(true);
				Ext.getCmp('btnView').setDisabled(true);
			}
			Ext.getCmp('btnDel').setDisabled(false);
			Ext.getCmp('btnLock').setDisabled(false);
		}else{
			Ext.getCmp('btnModify').setDisabled(true);
			Ext.getCmp('btnView').setDisabled(true);
			Ext.getCmp('btnDel').setDisabled(true);
			Ext.getCmp('btnLock').setDisabled(true);
		}
	}.createDelegate(this));

	this.on("rowdblclick", function(grid,rowIndex){
		this.showView(false);
	});

	var onContextMenu = function(grid,rowIndex,e){
		e.stopEvent();
		grid.getSelectionModel().selectRow(rowIndex);
		var messageContextMenu = new Ext.menu.Menu({
			id: 'messageContextMenu',
			items : [
			{
				id:'rAdd',
				text : c.add,
				iconCls:'add',
				handler: this.showWindow.createDelegate(this, [true])
			},{
				id:'rModify',
				text : c.modify,
				iconCls:'modify',
				handler:this.showWindow.createDelegate(this, [false])
			},{
				id:'rDel',
				text : c.del,
				iconCls:'remove',
				handler:remove.createDelegate(this)
			},{
				id:'rlock',
				text : c.lock,
				iconCls:'lock',
				handler:lock.createDelegate(this)
			}]
		});
		messageContextMenu.showAt(e.getXY());
		e.preventDefault();
	};
	this.on('rowcontextmenu',onContextMenu);
	
	//var acl = this.gridReader.jsonData.Acl;
	//current ACL

	//var acl = gridReader.jsonData.Acl;
	/*this.store.on('load',function(){
		
		if(typeof acl!="undefined"){
			var gridTb = this.getTopToolbar();
			if(acl[0].aNew===0){
				gridTb.items.get(0).hide();
				gridTb.items.get(1).hide();
				messageContextMenu.items.get(1).hide();
			}
			if(acl[0].aEdit===0){
				gridTb.items.get(2).hide();
				gridTb.items.get(3).hide();
				messageContextMenu.items.get(2).hide();
			}
			if(acl[0].aDel===0){
				gridTb.items.get(4).hide();
				gridTb.items.get(5).hide();
				messageContextMenu.items.get(3).hide();
			}
		}
	}.createDelegate(this));
*/
};
Ext.extend(SysHelpDataCore,Ext.ux.grid.GridPanel);